package com.xy.novelmanagement.controller;

import com.xy.model.CommentRules;
import com.xy.model.PageList;
import com.xy.model.RestfulResult;
import com.xy.novelmanagement.service.ICommentRuleManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/commentRule")
public class CommentRuleManagementController {
		@Autowired
		private ICommentRuleManagementService commentRuleService;

		@GetMapping("/")
		public RestfulResult getList(Integer pageSize, Integer pageNum) {
				pageNum = pageNum == null ? 1 : pageNum;
				pageSize = pageSize == null ? 10 : pageSize;
				if (pageNum <= 0) {
						pageNum = 1;
				}
				if (pageSize <= 0 || pageSize >= 100) {
						pageSize = 10;
				}
				PageList<CommentRules> commentList = commentRuleService.getList(pageNum, pageSize);
				return RestfulResult.success(commentList);
		}

		@PostMapping("/")
		public RestfulResult addRuleList(String ruleName, String[] ruleList) {
				log.info("CommentRuleManagementController addRuleList ruleName = " + ruleName);
				log.info("CommentRuleManagementController addRuleList ruleList = " + Arrays.toString(ruleList));
				if (ruleName == null || (ruleName.trim()).equals("")) {
						return RestfulResult.failed("-403", "规则名不能为空！");
				}
				if (ruleList == null || ruleList.length == 0) {
						return RestfulResult.failed("-403", "规则集不能为空！");
				}
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ruleName", ruleName);
				map.put("ruleList", ruleList);
				Boolean b = true;
				b = commentRuleService.addRule(map);
				log.info("添加{}规则集成功", ruleName);
				return RestfulResult.success(ruleList);
		}

		@DeleteMapping("/{ruleNames}")
		public RestfulResult deleteRules(@PathVariable String ruleNames) {
				log.info("deleteRules ruleNames = " + ruleNames);
				ruleNames = ruleNames.trim();
				if (StringUtils.isEmpty(ruleNames)) {
						return RestfulResult.failed("-403", "删除的规则不能为空");
				}
				if (ruleNames.split(",").length == 0) {
						return RestfulResult.failed("-403", "传入参数不正确");
				}
				String delRuleNames[] = ruleNames.split(",");
				Boolean result = commentRuleService.deleteRulesByName(delRuleNames);
				if (result) {
						return RestfulResult.success();
				} else {
						return RestfulResult.failed("-200", "删除失败");
				}
		}

		@PutMapping("/")
		public RestfulResult updateRule(String oldRuleName,String newRuleName,String[] ruleList){
				log.info("CommentRuleManagementController updateRule oldRuleName = " + oldRuleName);
				log.info("CommentRuleManagementController updateRule newRuleName = " + newRuleName);
				log.info("CommentRuleManagementController updateRule ruleList = " + Arrays.toString(ruleList));
				if (oldRuleName == null || (oldRuleName.trim()).equals("")) {
						return RestfulResult.failed("-403", "旧规则名不能为空！");
				}
				/*if (oldRuleName.equals(newRuleName)){
						return RestfulResult.failed("-403","新规则名不能和旧规则名相同");
				}*/
				if (ruleList == null || ruleList.length == 0) {
						return RestfulResult.failed("-403", "规则集不能为空！");
				}
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("oldRuleName", oldRuleName.trim());
				map.put("newRuleName", newRuleName.trim());
				map.put("ruleList", ruleList);
				Boolean b;
				b = commentRuleService.updateRule(map);
				if(b){
						log.info("更新{}规则集成功", newRuleName);
						return RestfulResult.success(ruleList);
				}else {
						log.info("更新{}规则集成功", newRuleName);
						return RestfulResult.failed("404","更新失败！");
				}

		}
}
